package com.ttg.utils.connection;

/**
 * Class TTGHttpClientException
 * Description
 * Create 2015-09-10 20:23:42
 * @author Ardy
 */
public class TTGHttpClientException extends Exception {

    /**
     * Constructs TTGHttpClientException
     * Description
     */
    private TTGHttpClientException() {}

    /**
     * Constructs TTGHttpClientException
     * Description
     *
     * @param msg
     */
    private TTGHttpClientException(String msg) {
        super(msg);
    }

    /**
     * Constructs TTGHttpClientException
     * Description
     *
     * @param cause
     */
    public TTGHttpClientException(Throwable cause) {
        super(cause);
    }

    /**
     * Constructs TTGHttpClientException
     * Description
     *
     * @param msg
     * @param cause
     */
    public TTGHttpClientException(String msg, Throwable cause) {
        super(msg, cause);
    }

    /**
     * Method getMessage
     * Description
     *
     * @return String
     */
    public String getMessage() {
        return buildMessage(super.getMessage(), this.getCause());
    }

    /**
     * Method getRootCause
     * Description
     *
     * @return Throwable
     */
    public Throwable getRootCause() {
        Throwable rootCause = null;

        for (Throwable cause = this.getCause(); (cause != null) && (cause != rootCause); cause = cause.getCause()) {
            rootCause = cause;
        }

        return rootCause;
    }

    /**
     * Method getMostSpecificCause
     * Description
     *
     * @return Throwable
     */
    public Throwable getMostSpecificCause() {
        Throwable rootCause = this.getRootCause();

        return (Throwable) ((rootCause != null)
                            ? rootCause
                            : this);
    }

    /**
     * Method contains
     * Description
     *
     * @param exType Class<?>
     *
     * @return boolean
     */
    public boolean contains(Class<?> exType) {
        if (exType == null) {
            return false;
        } else if (exType.isInstance(this)) {
            return true;
        } else {
            Throwable cause = this.getCause();

            if (cause == this) {
                return false;
            } else if (cause instanceof TTGHttpClientException) {
                return ((TTGHttpClientException) cause).contains(exType);
            } else {
                while (cause != null) {
                    if (exType.isInstance(cause)) {
                        return true;
                    }

                    if (cause.getCause() == cause) {
                        break;
                    }

                    cause = cause.getCause();
                }

                return false;
            }
        }
    }

    /**
     * Method buildMessage
     * Description
     *
     * @param message String
     * @param cause Throwable
     *
     * @return String
     */
    public static String buildMessage(String message, Throwable cause) {
        if (cause != null) {
            StringBuilder sb = new StringBuilder();

            if (message != null) {
                sb.append(message).append("; ");
            }

            sb.append("nested exception is ").append(cause);

            return sb.toString();
        } else {
            return message;
        }
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
